#include "blldataflow.h"
#include "globaldef.h"

BllDataFlow *BllDataFlow::instance = nullptr;

BllDataFlow::BllDataFlow(QObject *parent) : QObject(parent)
{
    initRegExpValidator();

    myofficeThread = new QThread();
    myoffice = new MyOffice();
    myoffice->moveToThread(myofficeThread);
    //Signals and Slots
    connect(this, &BllDataFlow::generateAllHtmlSignal, myoffice, &MyOffice::generateAllHtml);
    connect(myoffice, &MyOffice::generateAllHtmlSucceedSignal, this, &BllDataFlow::generateAllHtmlSucceedSignal);
    connect(this, &BllDataFlow::savePdfSignal, myoffice, &MyOffice::savePdfSlot);
    connect(myoffice, &MyOffice::savePdfSucceedSignal, this, &BllDataFlow::savePdfSucceedSignal);
    connect(this, &BllDataFlow::updatePrintParamSignal, myoffice, &MyOffice::updatePrintParamSlot);

    myofficeThread->start();

}

BllDataFlow * BllDataFlow::getInstance()
{
    static QMutex mutex;
    if(!instance)
    {
        QMutexLocker locker(&mutex);
        if(!instance)
        {            
            instance = new BllDataFlow();            
        }
    }
    return instance;
}

void BllDataFlow::returnLastMainPage()
{
    emit returnLastMainPageSignal();
}

void BllDataFlow::changeMainPageIndex(int index)
{
    emit changeMainPageIndexSignal(index);
}

void BllDataFlow::changeConfigPageIndex(int index)
{
    emit changeConfigPageIndexSignal(index);
}

void BllDataFlow::changeExperimentPageIndex(int index)
{
    emit changeExperimentPageIndexSignal(index);
}

void BllDataFlow::changeAboutPageIndex(int index)
{
    emit changeAboutPageIndexSignal(index);
}

void BllDataFlow::changeSystemConfigPageIndex(int index)
{
    emit changeSystemConfigPageIndexSignal(index);
}

void BllDataFlow::changeFactoryConfigPageIndex(int index)
{
    emit changeFactoryConfigPageIndexSignal(index);
}

void BllDataFlow::changeDataPageIndex(int index)
{
    emit changeDataPageIndexSignal(index);
}

void BllDataFlow::showReportPage(const ExperimentInfo &expInfo)
{
    emit showReportPageSignal(expInfo);
}

void BllDataFlow::setUserStatusText(QString username)
{
    emit setUserStatusTextSignal(username);
}

void BllDataFlow::showAllParameters(bool scrollToBottom)
{
    emit showAllParametersSignal(scrollToBottom);
}

void BllDataFlow::loadEnabledParamters()
{
    emit loadEnabledParamtersSignal();
}

void BllDataFlow::setWidgetVisible(int permission)
{
    emit setWidgetVisibleSignal(permission);
}

void BllDataFlow::showAllUsers(bool scrollToBottom)
{
    emit showAllUsersSignal(scrollToBottom);
}

void BllDataFlow::showAllRoles(bool scrollToBottom)
{
    emit showAllRolesSignal(scrollToBottom);
}

void BllDataFlow::updateVoltTestResult(VoltTest vt)
{
    emit updateVoltTestResultSignal(vt);
}

void BllDataFlow::updateFlowTestResult(FlowTest st)
{
    emit updateFlowTestResultSignal(st);
}

void BllDataFlow::insertExpInfo(const ExperimentInfo &expInfo)
{
    emit insertExpInfoSignal(expInfo);
}

void BllDataFlow::updateExpPassInfo(const ExperimentInfo &expInfo)
{
    emit updateExpPassInfoSignal(expInfo);
}

void BllDataFlow::updateExpFailInfo(const ExperimentInfo &expInfo, const HostReply &error, int errortype)
{
    emit updateExpFailInfoSignal(expInfo, error, errortype);
}

void BllDataFlow::setLineEditRoleName(QString name)
{
    emit setLineEditRoleNameSignal(name);
}

void BllDataFlow::updatePermissionListWidget(int permission)
{
    emit updatePermissionListWidgetSignal(permission);
}

void BllDataFlow::sendDeletedRole(Role role)
{
    emit sendDeletedRoleSignal(role);
}

void BllDataFlow::sendModifyRoleUser(User user)
{
    emit sendModifyRoleUserSignal(user);
}

void BllDataFlow::sendResetPwUser(User user)
{
    emit sendResetPwUserSignal(user);
}

void BllDataFlow::sendDeleteUser(User user)
{
    emit sendDeleteUserSignal(user);
}

void BllDataFlow::sendModifyParameter(Parameter p)
{
    sendModifyParameterSignal(p);
}

void BllDataFlow::sendDeleteParameter(Parameter p)
{
    sendDeleteParameterSignal(p);
}

void BllDataFlow::setPowerOffEnable(bool state)
{
    emit setPowerOffEnableSignal(state);
}

void BllDataFlow::generateAllHtml(int type, const QString contentHtml)
{
    emit generateAllHtmlSignal(type, contentHtml);
}

void BllDataFlow::savePdf(int type, const QString fileName, const QString contentHtml)
{
    emit savePdfSignal(type, fileName, contentHtml);
}

void BllDataFlow::retranslateUi()
{
    emit retranslateUiSignal();
}

void BllDataFlow::selectDateTimePicker(QDateTime dateTime, QString whatsThis)
{
    emit selectDateTimePickerSignal(dateTime, whatsThis);
}

void BllDataFlow::buildFinished()
{
    emit buildFinishedSignal();
}

void BllDataFlow::logout()
{
    emit logoutSignal();
}

void BllDataFlow::changePwDone()
{
    emit changePwDoneSignal();
}

void BllDataFlow::endExperiment(bool state)
{
    emit endExperimentSignal(state);
}

//void BllDataFlow::returnMain(bool state)
//{
   // emit returnMainSignal(state);
//}

void BllDataFlow::setCurrentTime(int type)
{
    emit setCurrentTimeSignal(type);
}

void BllDataFlow::initRegExpValidator()
{
    //length = [3,15],  letters and digits
    gUsernameRegExp.setPattern("^[a-zA-Z0-9]{1,15}$");
    gUsernameRegExp.setCaseSensitivity(Qt::CaseSensitive);
    gUsernameValidator = new QRegExpValidator(gUsernameRegExp);

    //length = [6,15], non-whitespace characters
    gPwRegExp.setPattern("\\S{6,15}");
    gPwRegExp.setCaseSensitivity(Qt::CaseSensitive);
    gPwValidator = new QRegExpValidator(gPwRegExp);

    //underline, English and Chinese char and digits
    gParamNameRegExp.setPattern("^[a-zA-Z0-9_\u4e00-\u9fa5\\w]+$");
    gParamNameRegExp.setCaseSensitivity(Qt::CaseSensitive);
    gParamNameValidator = new QRegExpValidator(gParamNameRegExp);

    //length = [1,6] English char and digits
    gParamCodeRegExp.setPattern("^[A-Za-z0-9]{1,6}$");//"^[A-Za-z0-9]+$"
    gParamCodeRegExp.setCaseSensitivity(Qt::CaseSensitive);
    gParamCodeValidator = new QRegExpValidator(gParamCodeRegExp);

    // non-whitespace characters
    gSerialCodeRegExp.setPattern("^[\\S]+$");
    gSerialCodeRegExp.setCaseSensitivity(Qt::CaseSensitive);
    gSerialCodeValidator = new QRegExpValidator(gSerialCodeRegExp);
}
